/**
 * @ author: 汪孟苹
 */
define(["jquery","bbcare","fu/app/comm/comm",
    "scprit/followup/app/surveyStageAssess/surveyStagePatient.servers.js",
    "scprit/followup/app/surveyStageAssess/surveyStagePatient.ui.js"],
    function ($,$bb,$comm,$surveyStagePatientServers,$surveyStagePatientUi) {
    var event = {};
    /**
     * 选中某患者，进行跳转
     */
    var onPatientsSelect = function(){
        var data = $(this).data("value");
        $("." + data.className).modal("hide");
        var patientId = $(this).attr("patientId");
        var patientName = $.trim($(this).find("span.patient-name").text());
        data = $.extend(data,{"patientId":patientId,"patientName":patientName});
        $bb.openPage(data.openPage,data);
    }
    /**
     * 即时搜索成功后的回调函数
     */
    var ajaxPatientsCallBack = function(rows,data){
        $(".searchBox .searchResult").css({"display":"block"});
        $surveyStagePatientUi.patentsInit({
            "id":"searchResult",
            "list":rows
        });
        $("#searchResult .patient-item").data({"value":data});
        $("#searchResult .patient-item").click(onPatientsSelect);
    }
    /**
     * 即时搜索（根据姓名和手机号码搜索患者）
     */
    var onSearchInput = function(){
        var data = $(this).data("value");
        var reg = /^$|^1[3456789]\d{9}$/;
        var value = $(this).val();
        var params = { "flag":2 };
        params = reg.test(value) ? $.extend(params,{"phoneNum":value}) : $.extend(params,{"patientName":value});
        if(value != ""){ 
            $surveyStagePatientServers.ajaxPatients(params,ajaxPatientsCallBack,data);
        }else{
            $("#searchResult").html("");
        }
    }
    /**
     * 清空搜索框
     */
    var onSearchInputEmpty = function(){
        var value = $(this).parent().find("input").val("");
        $("#searchResult").html("");
    }
    /**
     * 点击“新增评估问卷”后的click事件
     */
    event.onAddSurveyStageQuestBtn = function(){
        var data = $(this).data("value");
        var className = "surveyStageModal";
        $comm.modal({
            "className":className,
            "tit":"新增阶段问卷"
        },function(){
            $surveyStagePatientUi.init({
                "dom":$("." + className + " .modal-body")
            });          
            $("." + className).modal('show');
            $.extend(data,{"className":className});
            $("."+className + " .searchBox input").data({"value":data});
            $("."+className + " .searchBox input").bind("input propertychange",onSearchInput);
            $("."+className + " .searchBox .input-group-addon").bind("click",onSearchInputEmpty);
        });
    }

    return event;
});